-- UIAirShipGainEffect
-- Create by huanghr Jun/1/2016
-- 获得飞艇

-- UIAirShipGainEffect继承自Layer
UIAirShipGainEffect = class("UIAirShipGainEffect", function()
    return cc.Layer:create();
end);

function UIAirShipGainEffect.create(shipId)
    return UIAirShipGainEffect.new(shipId);
end

-- 内部函数声明
local resize;
local redraw;
local redrawStar;
local registerTouchEvent;

-- 构造函数
function UIAirShipGainEffect:ctor(shipId)
    -- 初始化
    self:setName("UIAirShipGainEffect");
    local node = cc.CSLoader:createNode("layout/hero/HeroGainEffect.csb");
    self:addChild(node);
    self.node = node;
    self.shipId = shipId;

    local headImg = findChildByName(self.node, "CT/hero_image");
    self.initPosX = headImg:getPositionX();
    self.initPosY = headImg:getPositionY();

    -- 需要显示在头部菜单上面
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent(node);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 适配
    self:resize();
end

-- 隐藏冒泡
function UIAirShipGainEffect:hideBubble()
    local btnShare = findChildByName(self.node, "BT/btn_share");
    local hintNode = findChildByName(btnShare, "hint");
    checkBlueBubbleStatus(hintNode, false);
    setButtonEnabled(btnShare, false);
end

-- 注册事件处理回调函数
function UIAirShipGainEffect:registerEventCallback()
    -- 关注facebook分享成功的事件
    EventMgr.register("UIAirShipGainEffect", event.FB_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注line分享成功的事件
    EventMgr.register("UIAirShipGainEffect", event.LINE_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注微信分享成功的事件
    EventMgr.register("UIAirShipGainEffect", event.WECHAT_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注微博分享成功的事件
    EventMgr.register("UIAirShipGainEffect", event.WEIBO_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIAirShipGainEffect 界面析构清理");
            EventMgr.removeAll("UIAirShipGainEffect");
        end
    end);
end

-- 注册点击事件
function UIAirShipGainEffect:registerTouchEvent(node)
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAirShipGainEffect");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册任意地方点击返回事件
    local btnBgBack = findChildByName(self.node, "bg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAirShipGainEffect");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);

    -- 注册分享按钮点击事件
    local btnShare = findChildByName(self.node, "BT/btn_share");
    local hintNode = findChildByName(btnShare, "hint");
    local function onShareClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            self:doShare();
        end
    end
    btnShare:addTouchEventListener(onShareClick);
    TextStyleM.setTextStyle(btnShare, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnShare:setTitleText(getLocStr("btn_text_share"));
    checkBlueBubbleStatus(hintNode, true);

    if isShareFuncOpen() then
        btnShare:setVisible(true);
    else
        btnShare:setVisible(false);
    end
end

-- 分享
function UIAirShipGainEffect:doShare()
    local title   = getLocStr("airship_share_title");
    local content = getLocStr("airship_share_content");
    local shareTitle = "";
    local shareContent = "";
    if isOverseaPackage() then
        -- 海外版本
        shareTitle = string.format(getLocStr("fb_airship_share_title"), NewAirShipM.query(self.shipId, "name"));
        shareContent = PetM.query(self.shipId, "desc");
    else
        -- 国内版本
        shareContent = string.format(getLocStr("cn_airship_share_desc"), NewAirShipM.query(self.shipId, "name"));
    end

    local extra = { ["title"] = shareTitle, ["desc"] = shareContent, ["classId"] = self.shipId, };

    showShareForm(SHARE_TYPE_AIRSHIP, title, content, extra);
end

-- 适配
function UIAirShipGainEffect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIAirShipGainEffect:redraw()
    local CT = self.node:getChildByName("CT");

    local shipTalkLabel  = findChildByName(CT, "talk");

    local shipConfig = NewAirShipM.query(self.shipId);
    -- 显示飞船对话
    TextStyleM.setTextStyle(shipTalkLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW);
    shipTalkLabel:setString(shipConfig.talk);

    -- 创建飞船信息
    self:createShipModel();

    -- 重绘招募部分信息
    self:redrawPetRecruitNode();
end

-- 重绘招募部分信息
function UIAirShipGainEffect:redrawPetRecruitNode()
    local petNode = findChildByName(self.node, "CT/pet_recruit");
    local petTop  = findChildByName(petNode, "TOP");
    local petCT   = findChildByName(petNode, "CT");
    local petBT   = findChildByName(petNode, "BT");

    petTop:setVisible(false);
    petCT:setVisible(false);
    petBT:setVisible(false);

    -- 获取阵营信息
    local campId = NewAirShipM.query(self.shipId, "camp");

    -- 背景
    local bg = findChildByName(petNode, "bg/hero_bg");
    local campIconPath = getHeroBgIconPath("gain_ship_bg");
    bg:loadTexture(campIconPath);

    -- 效果
    local effectImg = findChildByName(petNode, "bg/effect_img");
    effectImg:loadTexture(getMercenaryCampIconPath("effect" .. campId));
    effectImg:stopAllActions();
    effectImg:removeAllChildren();
    local effectList = {1287, 1286, 1288, 1289};
    playEffect(effectImg, effectList[campId], 300, 250, nil, 2.35);
end

-- 飞艇名字特效
function UIAirShipGainEffect:showTitleEffect(textNode)

    local str = textNode:getString();
    local fontSize = 70 * getLocFontSizeMulti();
    local bgPath = "images/ui/text_mask/text_mask.png";

    -- 生成艺术字并旋转飞入
    genArtWordAndRotateFlyIn(textNode, fontSize, bgPath);
end

-- 创建英雄模型
function UIAirShipGainEffect:createShipModel()
    local headImg = findChildByName(self.node, "CT/hero_image");
    headImg:setPositionX(DESIGN_WIDTH);
    local lightImg = findChildByName(self.node, "CT/light_bg");
    lightImg:setVisible(false);
    local icon = NewAirShipM.query(self.shipId, "icon");

    local shipImg = getAirShipImgPath(icon);
    headImg:loadTexture(shipImg);
    headImg:setVisible(true);
    playFluctuateAnima(headImg, 0.1, 1);

    local posX = self.initPosX;
    local posY = self.initPosY;
    -- local accelarateFactor = 1;


    local callfunc = cc.CallFunc:create(function()
        local shipNameLabel  = findChildByName(self.node, "CT/name");
        local shipName = NewAirShipM.query(self.shipId, "name");
        -- 获得飞艇信息
        shipNameLabel:setString(shipName .. "!");
        shipNameLabel:setTextColor(TextStyleM.TEXT_COLOR_BROWN_YELLOW);
        self:showTitleEffect(shipNameLabel);
    end)

    local moveTo = cc.MoveTo:create(0.5, cc.p(posX, posY));
    headImg:runAction(cc.Sequence:create(moveTo, callfunc));

end
